package classes;

import java.awt.image.BufferedImage;


/**
 * Noise Reduction Class
 * 
 * @author Nicole Nelson
 */
public class NoiseReduction 
{	
	private int BLACK = -16777216;
	private int WHITE = -1;
	
	/*
	 * Uses a simple algorithm to try and help reduce the noise that is in the image
	 */
	public BufferedImage reduceNoise(BufferedImage image)
	{
		
		int imgHeight = image.getHeight();
		int imgWidth = image.getWidth();
		
		for (int height = 0; height < imgHeight; height++)
			for (int width = 0; width < imgWidth; width++)
			{
				if(height < 1 || width < 1 || height > imgHeight - 2 || width > imgWidth - 2)
					image.setRGB(width, height, image.getRGB(width, height));
				else if((image.getRGB(width - 1, height) == WHITE &&
				   image.getRGB(width + 1, height) == WHITE) ||
				   (image.getRGB(width, height - 1) == WHITE &&
				   image.getRGB(width, height + 1) == WHITE))
					image.setRGB(width, height, WHITE);
				else if((image.getRGB(width - 1, height) == BLACK &&
						   image.getRGB(width + 1, height) == BLACK) ||
						   (image.getRGB(width, height - 1) == BLACK &&
						   image.getRGB(width, height + 1) == BLACK))
					image.setRGB(width, height, BLACK);	
				else
					image.setRGB(width, height, image.getRGB(width, height));
			}
		return image;
	}
}